<?php
/**
 * Created by PhpStorm.
 * User: bg
 * Date: 2019-01-22
 * Time: 上午 11:14
 */
namespace app\common\model;


use think\Db;
use think\Model;


//申报列表
class ReportModel extends Model{
    /**
     *  分页查询申报数据(不查询逻辑删除的数据)
     * @param array $where     AND查询条件
     * @param array $whereOr   OR查询条件
     * @return array
     * @throws \think\exception\DbException
     * @throws db\exception\DataNotFoundException
     * @throws db\exception\ModelNotFoundException
     */
    public function selectPageSize($where=array(),$whereOr=array())
    {
        // 获取分页配置
        $currentPage = !empty($where['page'])?$where['page']:1;
        unset($where['page']);
        $Configuration = new Configuration();
        $listRows = intval($Configuration->getkey('pagesize'));

        // 连表
        $join = [
            ['test cm','cm.user_id = r.user_id','left']
        ];
        $field = 'r.*,cm.realname';

        $page = Db::name('report')
            ->alias('r')
            ->join($join)
            ->where($where)
            ->whereOr($whereOr)
            ->field($field)
            ->page($currentPage,$listRows)
            ->order('r.add_time desc')
            ->select();
        $data = array();
        // 数量查询
        $data['page']['count'] = Db::name('report')->alias('r')->where($where)->whereOr($whereOr)->count();

        // 获取当前页
        $data['page']['currentPage'] = $currentPage;
        // 计算共有多少页
        $data['page']['lastPage'] = intval(ceil($data['page']['count'] / $listRows));;
        // 每页显示数目
        $data['page']['listRows'] = $listRows;

        $data['data'] = $page;

        return $data;
    }

    /**
     *  分页查询申报数据(不查询逻辑删除的数据)
     * @param array $where     AND查询条件
     * @param array $whereOr   OR查询条件
     * @return array
     */
    public function selectJoinPageSize($where=array(),$whereOr=array())
    {
        // 获取分页配置
        $currentPage = !empty($where['page'])?$where['page']:1;
        unset($where['page']);
        $Configuration = new Configuration();
        $listRows = intval($Configuration->getkey('pagesize'));

        // 连表
        $join = [
            ['customer_administrative_center cd', 'cd.user_id = r.user_id', 'left'],
            ['test cm','cm.user_id = r.user_id','left']
        ];
        $field = 'r.*,cm.realname';

        $page = Db::name('report')
            ->alias('r')
            ->join($join)
            ->where($where)
            ->whereOr($whereOr)
            ->field($field)
            ->page($currentPage,$listRows)
            ->order('r.add_time desc')
            ->select();
        $data = array();
        // 数量查询
        $data['page']['count'] = Db::name('report')->alias('r')->join($join)->where($where)->whereOr($whereOr)->count();

        // 获取当前页
        $data['page']['currentPage'] = $currentPage;
        // 计算共有多少页
        $data['page']['lastPage'] = intval(ceil($data['page']['count'] / $listRows));;
        // 每页显示数目
        $data['page']['listRows'] = $listRows;

        $data['data'] = $page;

        return $data;
    }

    /**
     * 报备记录增加一条数据(返回新增加的id)
     * @param $data  ->  数组   ['key' => 'value']
     * @return id
     */
    public function insertOne($data)
    {
        $id = Db::name('report')->insertGetId($data);
        // 判断结果 并返回
        if ($id == false) {
            return false;
        }
        return $id;
    }

    //获取报备详情
    public function getJoinOne($where,$filds='*'){
        $join = [
            ['test cm','cm.user_id = r.user_id','left']
        ];
        $ret = Db::name('report')->alias('r')
            ->where($where)
            ->join($join)
            ->field($filds)
            ->find();
        return $ret;
    }

    //更新报备
    public function updateReport($where,$data){
        $ret = Db::name('report')->where($where)->update($data);
        return $ret;
    }
}